package top.mayyd.producteurekaconsumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.mayyd.producteurekaconsumer.service.ProductService;
import top.mayyd.producteurekaservice.domain.Product;

@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;

    //produces 可以设置返回的数据结构，比如json、xml
    /*@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Product show(@PathVariable("id") Long id) {
        return productService.getById(id);
        //return productService.getOne(id);
    }*/

    //降级处理
    @HystrixCommand(commandKey = "Product-Show", fallbackMethod = "showFallback") //声明一个失败时访问的函数
    @GetMapping(value = "/{id}")
    public String show(@PathVariable("id") Long id) {
        //return productService.getById(id);
        return productService.getOne(id);
        //return productService.getByFeign(id);
    }

    public String showFallback(Long id) {
        return "当前人数过多，请稍后再试试吧~";
    }
}
